// 操作人ID
var tokenJson = JSON.parse(decodeURIComponent(web.readCookie("loginData")));

var comPageNum = 1;//分页公共页码
/*todo 新增服務商合併 完成*/
function improveInfo(providerId) {
    var addProviderPageUrl = '';
    if(providerId) {
        addProviderPageUrl = "commonAddPage.html?type=edit&providerId="+providerId
    }else {
        addProviderPageUrl = "commonAddPage.html";
    }
    var index = layer.open({
        type: 2,
        title: "新增服务商",
        content: addProviderPageUrl
    });
    layer.full(index);
}
function viewDetails(providerId,status) {
    var index = layer.open({
        type: 2,
        title: "查看服务商",
        content: "commonDetailPage.html?providerId="+providerId+"&status="+status
    });
    layer.full(index);
}
function searchList(pageNum) {
    var params = {
        "number": $(".providerId").val(),//服务商代码
        "channelNumber": $(".channelName").attr("data-number"),//合作渠道
        "companyId": $(".companyName").attr("data-id"),//主维护单位
        "name": $(".providerName").val(),//服务商名称
        "pageNumber": pageNum,
        "pageSize": 20,
        "status": $(".status").val(),//状态（0:待启用,1,启用,2:禁用）
        "customerNumber": $(".customerNumber").val(),//客户代码
        "customerName": $(".customerName").val(),//客户名称
    }
    web.doAjax(root.approvedServiceProviders.findProviderByPage, 'post',params, function (data) {
       if(data.code == 0){
           $('.M-box3').pagination({
               pageCount:data.totalPage,
               current:pageNum,
               jump: true,
               coping: true,
               prevContent: '上页',
               nextContent: '下页',
               callback: function (api) {
                   comPageNum = api.getCurrent();
                   searchList(api.getCurrent());
               }
           });
           renderList(data.result);
       }else {

       }
    },1);
}
function renderList(data) {
    var listHtml = "";
    data.forEach((item,index) => {
        if(item.status == 0){
            var btnHtml = "<a onclick='improveInfo("+item.id+")'>完善资料</a>";
        }else {
            var btnHtml = "<a onclick='viewDetails("+item.id+"," + item.status + ")'>查看详情</a>";
        }
        var cooperationChannel = (item.cooperationChannelList==null)?"":item.cooperationChannelList.join(",");
        listHtml += '<tr class="text-c">\n' +
            '                    <td>'+(index+1)+'</td>\n' +
            '                    <td title="'+item.name+'">'+item.name+'</td>\n' +
            '                    <td title="'+item.number+'">'+item.number+'</td>\n' +
            '                    <td title="'+cooperationChannel+'">'+cooperationChannel+'</td>\n' +
            '                    <td title="'+item.companyName+'">'+item.companyName+'</td>\n' +
            '                    <td title="'+item.createDate+'">'+item.createDate+'</td>\n' +
            '                    <td>'+item.statusName+'</td>\n' +
            '                    <td>'+btnHtml+'</td>\n' +
            '                </tr>';
    });
    $("#datatable .table-tbody").html(listHtml);
}
/*重置表单*/
function resetFormFn() {
    $(".channelName").attr({"data-id":"","data-number":""});
    $(".companyName").attr("data-id","");
}
/*服务商*/
function searchProviderListFn(el,type){
    if(type&&type=="inp"){
        $(el).attr("data-id","");
    }
    var params = {
        "name":el.value,
    }
    web.doAjax(root.approvedServiceProviders.findAll,'post',params, function (data) {
        if(data.code == "0") {
            $(".providerList").css("display","block");
            var providerHtml = '';
            if(!data.data||data.data.length == 0) {
                providerHtml = '<li class="clearfix pl-10 mb-5">无数据</li>';
            } else {
                data.data.forEach((item) => {
                    providerHtml += "<li class='clearfix pl-10 mb-5' onclick='selectProviderListItem("+JSON.stringify(item)+")'>"+item.name+"</li>"
                })
            }
            $(".providerList").html(providerHtml);
        }
    },2);
}
/*合作渠道*/
function searchChannelListFn(el,type) {
    if(type&&type=="inp"){
        $(el).attr({"data-id":"","data-number":""});
    }
    var params = {
        "name":el.value,
    }
    web.doAjax(root.approvedServiceProviders.findChannelFromDockListByName,'post',params, function (data) {
        if(data.code == "0") {
            $(".channelList").css("display","block");
            var channelHtml = '';
            if(!data.data||data.data.length == 0) {
                // todo 提取公共变量-完成
                channelHtml = '<li class="clearfix pl-10 mb-5">无数据</li>';
            } else {
                data.data.forEach((item) => {
                    channelHtml += "<li class='clearfix pl-10 mb-5' onclick='selectChannelItem("+JSON.stringify(item)+")'>"+item.channelName+"</li>"
                })
            }
            $(".channelList").html(channelHtml);
        }
    },2);
}
/*主维护单位（客户归属）*/
function searchOrgListFn(el,type) {
    if(type&&type=="inp"){
        $(el).attr("data-id","");
    }
    var params = {
        "pageSize":100,
        "pageNum":1,
        "organizationName":el.value
    }
    web.doAjax(root.member.newSearchOrgList,'post',params, function (data) {
        if(data.code == "0") {
            $(".companyList").css("display","block");
            var companyHtml = '';
            if(data.data.length == 0) {
                companyHtml = '<li class="clearfix pl-10 mb-5">无数据</li>';
            } else {
                data.data.forEach((item) => {
                    companyHtml += "<li class='clearfix pl-10 mb-5' onclick='selectItem("+JSON.stringify(item)+")'>"+item.companyName+"</li>"
                })
            }
            $(".companyList").html(companyHtml);
        }
    },1);
}
$(document).bind("click", function(e) {
    //id为menu的是菜单，id为open的是打开菜单的按钮
    if($(e.target).closest(".companyList").length == 0 && $(e.target).closest(".companyName").length == 0) {
        //点击id为menu之外且id不是不是open，则触发
        $(".companyList").hide();
    }
    if($(e.target).closest(".providerList").length == 0 && $(e.target).closest(".providerName").length == 0){
        $(".providerList").hide();
    }
    if($(e.target).closest(".channelList").length == 0 && $(e.target).closest(".channelName").length == 0){
        $(".channelList").hide();
    }
});
function selectProviderListItem(data) {
    $(".providerName").val(data.name).attr("data-id",data.number);
    $(".providerList").hide();
}
function selectChannelItem(data){
    $(".channelName").val(data.channelName).attr({"data-id":data.channelId,"data-number":data.channelNumber});
    $(".channelList").hide();
}
function selectItem(data) {
    $(".companyName").val(data.companyName).attr("data-id",data.companyId);
    $(".companyList").hide();
}

//已准入服务商列表导出
function providerExport()
{
    var providerId = $(".providerId").val();
    var channelNumber = $(".channelName").attr("data-id");
    if (!channelNumber) {
        channelNumber = "";
    }
    var companyId = $(".companyName").attr("data-id");
    if (!companyId) {
        companyId = "";
    }
    var providerName = $(".providerName").val();
    var status = $(".status").val();
    var customerNumber = $(".customerNumber").val();
    var customerName = $(".customerName").val();
    var exportUrl = root.approvedServiceProviders.providerExport + "?number=" + providerId + "&channelNumber=" + channelNumber + "&companyId=" + companyId + "&name=" + providerName + "&status=" + status + "&customerNumber=" + customerNumber + "&customerName=" + customerName;
    window.location.href = exportUrl;
}